const api = require('../../utils/api.js');
const app = getApp();

let is_more = false;
Page({
  data: {
    show: false,
    unit: '',
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '暂无明细',
    },
    page: 1,
    is_more: true,
    isShowLoading: false,
    list: [],
    img_url: app.imgUrl,
    is_show_line: false,
    isShowMoreBtn: false,
  },

  onLoad(options) {
    let that = this;
    let isMulti =
      app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isMulti; // 是否为多店
    let isBrand =
      app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isBrand; // 是否为品牌版
    let opts = options || {};
    let { type = 0 } = opts;
    wx.setNavigationBarTitle({
      title: Number(type) === 0 ? '余额' : '积分',
    });
    this.setData({
      opts,
      type,
      isMulti,
      isBrand,
    });
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
  },

  getData() {
    let that = this;
    let { type, is_more, page, list } = that.data;
    if (is_more) {
      wx.showLoading({
        title: '正在加载...',
        mask: true,
      });
      app.ws_micro_shoprequest({
        url: Number(type) === 0 ? api.recharge.balance_list : api.recharge.integral_list,
        data: {
          page,
        },
        success: function(res) {
          if (res.code === 0) {
            let resp = res.data;
            let { list: new_list = [], type_list = [] } = resp;
            that.setData({
              type_list: Number(type) === 0 ? JSON.parse(type_list) : [],
              total_value: Number(type) === 0 ? resp.money : resp.integral
            })
            if (new_list.length > 0) {
              page += 1;
              that.setData({
                page,
                list: list.concat(new_list),
              });
            } else {
              that.setData({
                is_more: false,
                is_show_line: page > 1 ? true : false,
              });
            }
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
            });
          }
        },
        complete: function () {
          wx.hideLoading();
          that.setData({
            isShowLoading: true,
          })
        }
      });
    }
  },

  onShow() {
    app.pageOnLoad(this);
    this.setData({
      page: 1,
      list: [],
      is_more: true,
      isShowLoading: false,
      is_show_line: false,
    })
    this.getData();
  },

  onReachBottom() {
    this.getData();
  },

  showmorebtn () {
    this.setData({
      isShowMoreBtn: !this.data.isShowMoreBtn,
    })
  },

  onNavigate(e) {
    let url = e.currentTarget.dataset.url;
    app.util.navigateTo(url);
  },

  collectFormId(e) {
    app.collectFormId(e);
  },
});
